package com.yoomiito.app.ui.yijiayou

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.oilcard.OilCardOrderDetail
import com.yoomiito.app.ui.RecyclerViewBaseActivity
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.L
import com.yoomiito.app.widget.MyItemDecoration
import com.yoomiito.app.widget.SpacingDecoration
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 加油卡余额明细
 */
class OilCardDetailActivity : RecyclerViewBaseActivity<OilCardDetailPresenter>(){

    companion object{
        fun start(context: Context){
            val intent = Intent(context,OilCardDetailActivity::class.java)
            context.startActivity(intent)
        }
    }
    private val mAdapter = OilCarDetailAdapter(null)

    override fun newP(): OilCardDetailPresenter = OilCardDetailPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mTitleView.setCenterText("油米明细")

        mSmartRefreshLayout.setPadding(0, 0, 0, 0)
        val linearLayoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        mRecyclerView.layoutManager = linearLayoutManager
        mRecyclerView.addItemDecoration(MyItemDecoration(context, androidx.recyclerview.widget.RecyclerView.VERTICAL))
        mRecyclerView.adapter = mAdapter
    }

    override fun getNewData() {
        p.getData(currentPage)
    }

    fun setData(data: BaseList<OilCardOrderDetail>) {
        if (data.data != null) {
            if (data.current_page == 1) {
                if (data.data == null || data.data.size == 0) {
                    showEmptyView()
                } else {
                    mAdapter.setNewData(data.data)
                    showContentView()
                }
                mSmartRefreshLayout.finishRefresh(true)
                mSmartRefreshLayout.setNoMoreData(false)
            } else {
                mSmartRefreshLayout.finishLoadMore(true)
                mAdapter.addData(data.data)
            }
        } else {
            finishRefresh()
        }
        if (data.current_page==data.last_page) {
            mSmartRefreshLayout.finishLoadMoreWithNoMoreData()
        }
        currentPage = data.current_page
    }

    override fun getEmptyContent(): String = "无明细"

    override fun useMyItemDecoration(): Boolean = true

    fun finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true)
        mSmartRefreshLayout.finishLoadMore(true)
    }
}